<template>
    <el-dialog v-model="show" title="添加退货原因" width="30%" :before-close="handleClose">
        <div class="context">
            <el-form :model="state.tableData" label-width="120px" ref="searchFormRef">
                <el-form-item label="原因类型:" label-width="120px">
                    <el-input v-model="state.tableData.name"></el-input>
                </el-form-item>
                <el-form-item label="排序:" label-width="120px">
                    <el-input v-model="state.tableData.sort"></el-input>
                </el-form-item>
                <el-form-item label="是否启用:" label-width="120px">
                    <template #default="scope">
                        <el-switch v-model="state.tableData.status" :active-value="1" :inactive-value="0"
                            @change="changeHandle" />
                    </template>
                </el-form-item>
            </el-form>
        </div>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="close">取消</el-button>
                <el-button type="primary" @click="sure">确认</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
import { reactive, ref, toRefs, watch } from 'vue';
import { user } from '@/api/index'

interface State {
    tableData: returnReason
}
const searchFormRef = ref<FormInstance>()
const state = reactive<State>({
    tableData: {
        createTime: "",
        id: 0,
        name: "",
        sort: 0,
        status: 1
    },
})

// 父传子
const props = defineProps({
    show: {
        type: Boolean,
        default: false
    },
    data: {
        type: Number,
        default: 0
    }
})
// 子传父
const emit = defineEmits(["hide", "reach"])


// 点击黑屏和x按钮
const handleClose = (done: () => void) => {
    emit('hide')

}

// 取消按钮
const close = () => {
    emit('hide')
    console.log(props.data);
}
// 确认按钮
const sure = () => {
    if (props.data !== 0) {
        user.updateInforeturnReason(state.tableData).then(res => {
            ElMessage.success("编辑成功")
            emit('hide')
            emit('reach')
        })
    } else {
        user.AddreturnReason(state.tableData).then(res => {
            if (res.code == 200) {
                ElMessage.success("添加成功")
                emit('hide')
                emit('reach')
            }
        })
    }


}
// 是否启用
const changeHandle = () => {

}
watch(() => props.data, () => {
    if (props.data === 0) {
        state.tableData = {
            createTime: "",
            id: 0,
            name: "",
            sort: 0,
            status: 1
        }
        return
    }
    user.getreturnReason(props.data).then(res => {
        if (res.code === 200) {
            // 把数据赋值给 表单
            Object.assign(state.tableData, res.data) // api 没见过，就去看文档， Object.assign() ---> MDN
        }
    })
})
</script>

<style scoped>
.el-input {
    width: 300px;
}
</style>